package com.srit.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.srit.core.bean.Page;
import com.srit.system.bean.User;
import com.srit.system.dao.UserDao;
/**
 * @author chenxiling
 *user
 */
@Service
public class UserDaoImpl implements UserDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public int getMaxId() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void save(User t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(User t) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User getById(Integer id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void page(Page page) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public User getUserByName(String name) {
		String sql = "select ID,USERNAME,PASSWORD from TC_USER where username = ?";
		User user = jdbcTemplate.queryForObject(sql, new Object[]{name},new UesrRowMapper());
		return user;
	}
	class UserResultSetExtractor implements ResultSetExtractor {
		@Override
		public Object extractData(ResultSet rs) throws SQLException,
				DataAccessException {
			User user = new User();
			user.setUserId(rs.getInt("ID"));
			user.setUserName(rs.getString("USERNAME"));
			user.setPassword(rs.getString("PASSWORD"));
			return user;
		}

	}

	class UesrRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserResultSetExtractor extractor = new UserResultSetExtractor();
			return extractor.extractData(rs);
		}

	}
}
